(function($){

	//插件入口
	$.fn.tmValidator = function(options){
		this.each(function(){
			var opts = $.extend({},$.fn.tmValidator.defaults
				,$.fn.tmValidator.methods,options,$.fn.tmValidator.jvalidator);
			opts.init($(this),opts);
		});
	};
	
	//方法定义
	$.fn.tmValidator.methods = {
		init :function($form,opts){
			var $this = this;
			$form.find(".tmui_submit").on("click",function(){
				var flag =$this._validator($form,opts);
				if(flag){
					//回调函数
					if(opts.ajax){
						opts.callback($form);
					}else{
						$form.submit();
					}
				}else{
					return false;
				}
			});
			//打*
			$form.find("[trequired]").each(function(){
				$(this).before("<span class='need'>*</span>");
			});
		},
		_validator : function($form,opts){//空判断
			var $this = this;
			var flag = true;
			$form.find("[trequired],[tvalidator]").each(function(){
				var value = $(this).val();
				var reqiured = $(this).attr("trequired");
				if(isEmpty(value) && eval(reqiured)){
					$(this).css("border","2px solid red");
					$(this).focus();
					$this.showMessage($(this),$(this).attr("title"),"error");
					
					flag = false;
					return false;
				}else{
					$(this).css("border","1px solid #ccc");
					$this.showMessage($(this),"corrent");
				};
				
				var jvalidator = $(this).attr("tvalidator");
				//var flag = $.fn.tmValidator.jvalidator[jvalidator]($(this));
				if(isNotEmpty(value) && isNotEmpty(jvalidator) && !opts[jvalidator]($(this))){//必须不为空才进行验证
					$(this).focus();
					flag = false;
					return false;
				}else{
					return true;
				}

			});
			return flag;
		},
		showMessage : function($target,msg,flag){
			var $span = $target.next();
			if(isEmpty($span.html())){
				$span = $("<span></span>");
			}
			if(flag=="error"){
				$span.removeClass("corrent").addClass("error").text(msg);
			}else{
				$span.removeClass("error").addClass("corrent").text("正确");
			}
			$target.after($span);
		}
	};

	
	//属性参数
	$.fn.tmValidator.jvalidator = {
		email : function($target){
			return jxtvEmail($target);
		},
		telephone : function($target){
			return jxtvMobile($target);
		}
		
	};

	//默认参数
	$.fn.tmValidator.defaults = {
		ajax:true,
		callback:function($form){
			
		}
	
	};
	

	/**
	 * 判断非空
	 * 
	 * @param val
	 * @returns {Boolean}
	 */
	function isEmpty(val) {
		val = $.trim(val);
		if (val == null)
			return true;
		if (val == undefined || val == 'undefined')
			return true;
		if (val == "")
			return true;
		if (val.length == 0)
			return true;
		if (!/[^(^\s*)|(\s*$)]/.test(val))
			return true;
		return false;
	}

	function isNotEmpty(val) {
		return !isEmpty(val);
	}

	/*****************验证器**********************/
	
	/*固定电话号码*/
	function jxtvTelephone($target) {
		return  jxtvValidate($target, /^\d{3}-\d{8}|\d{4}-\d{7}$/, {
			msg :"电话号码不正确!"
		});
	};
	
	/*邮箱*/
	function jxtvEmail($target) {
		return  jxtvValidate($target, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, {
			msg :"邮箱格式不正确"
		});
	};
	
	/*手机*/
	function jxtvMobile($target) {
		return  jxtvValidate($target, /^(13|15|18)\d{9}$/, {
			msg :"手机号码不正确!"
		});
	};

	//控制所有的验证器的正则表达式判断
	function jxtvValidate($target,regex,msg){
		if(regex.test($target.val())){
			$.fn.tmValidator.methods.showMessage($target,"","current");
			return true;
		}else{
			$.fn.tmValidator.methods.showMessage($target,msg.msg,"error");
			return false;
		}
	}

})(jQuery)